#include <iostream>
#include <fstream>
#include <cstdlib>

#include "CurrentPair.h"

using namespace std;

/**
This class is utilized by the Neighboring class and holds a virus/spacer pair
that is currently being analyzed.  It takes an ifstream and reads in a new line
from the seeding file used in the previoius steps.  It also takes a locations
file which is used to find the spacer and virus seqeunces to be analyzed.
**/

/**TODO: have make map and get spacer be part of Locations so a new map isn't
         made on every single currentPair created **/

//Constructor: copies the locations file to the currentPair object, makes a map
//of spacer sequences so they can be retrieved by the getSpacer method, gets the
//new line and sets each parameter to the approiate value.

CurrentPair::CurrentPair(fstream &in, Locations m)
{
    //ctor
    //set locations and make spcrs map containing spacerAsc keys to spacerSeq
    //values.  Initailze vector containing pair
    l = new Locations(m);
//    spcrs  = makeMap();
    vector<string> vsPair;
    //getting new line and pushing back into vsPair vector
    char junk [1000];
    for (int i = 0; i<3; i++)
    {
        in.getline(junk, 1000, '\t');
        vsPair.push_back(junk);
        junk[0] = '\0';
    }
    in.getline(junk, 1000, '\n');
    vsPair.push_back(junk);
    junk[0] = '\0';
    //setting members of vsPair to each approaite section
    spacerAsc = vsPair[2];
    virusAsc = vsPair[0];
    spacer = l->getSpacer(spacerAsc);
    virus = l->getVirus(virusAsc);
    //calculate start position of protospacer within virus
    startPos = atoi(vsPair[1].c_str())-atoi(vsPair[3].c_str());
}

CurrentPair::~CurrentPair()
{
    //dtor
    delete l;
}

//returns spacer sequence
string CurrentPair::getSpacer()
{
    return spacer;
}

//returns virus sequence
string CurrentPair::getVirus()
{
    return virus;
}

//returns starting position
int CurrentPair::getStartPos()
{
    return startPos;
}

//returns virus ascession number
string CurrentPair::getVirusAsc()
{
    return virusAsc;
}

//returns spacera ascession numbers
string CurrentPair::getSpacerAsc()
{
    return spacerAsc;
}

void CurrentPair::printVector(vector<string> vec)
{
    for (int i = 0; i < vec.size(); i++)
    {
        cout << vec[i] << '\t';
    }
    cout << endl;
}

////retrival from some value based data structure that will let me get sequences with Asc as a key
////or break spacers into individual files
//string CurrentPair::getSpacer (string sAsc)
//{
//    map<string,string>::iterator it;
//    it = spcrs.begin();
//    it = spcrs.find(sAsc);
//    pair<string, string> sPair = *it;
//
//    return sPair.second;
//}

////gets virus sequence from ascession number
//string CurrentPair::getVirus (string asc)
//{
//    //opens file based on ascession number from approaite folder
//    char junk [1000];
//    ifstream in;
//    string str = l->getVirusLoc() + "/" + asc;
//    in.open(str.c_str());
//    while (in.is_open()==false)
//    {
//        cout<<"file not found"<<endl;
//    }
//    //toss header
//    in.getline(junk,1000,'\n');
//    //find length of sequence by going through file, close
//    int length = 0;
//    while(in.peek()!=EOF)
//    {
//        in>> junk[0];
//        length++;
//    }
//    in.clear();
//    in.close();
//    //reopen and get sequence based on length found above
//    in.open(str.c_str());
//    in.getline(junk, 1000, '\n');
//    int n = 0;
//    string seq;
//    while (n<length)
//    {
//        char save;
//        in >> save;
//        seq+=save;
//        n++;
//    }
//    in.clear();
//    in.close();
//    return seq;
//}
